package com.ssm.controller.admin;

import java.util.List;
import java.util.Map;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ssm.controller.SSMController;
import com.ssm.model.bean.SsmActions;
import com.ssm.model.bean.SsmUsers;
import com.ssm.model.mapper.ActionMapper;


/**
 * SSM Controller动作类
 * 
 * @author WTC<546895488@qq.com>
 * @version 2016/1/29
 * */
@Controller
@RequestMapping(value="Action")
public class ActionController  extends AdminBaseController  {
	
	
	ActionMapper am;
	/**
	 * 用户行为规则与日志管理界面
	 * @param request
	 * @param response
	 */
	@RequestMapping(value="index")
	public String UserAction(HttpServletRequest request)
	{
		
		ActionMapper am=M.getMapper(ActionMapper.class);
		am.selectAll();
		List<SsmActions> list=am.selectAll();
		this.List2Table(ActionMapper.class,null,request,false);	
		this.analysisAction("action_index", request);
		//request.setAttribute("list", list);
		request.setAttribute("WEB_TITLE", "用户行为管理界面");
		return "Action/index";
	}

	/**
	 * 用户行为规则与日志管理修改
	 * @param request
	 * @param response
	 */
	@RequestMapping(value="edit")
	public String ActionEdit(HttpServletRequest request,SsmActions action,HttpSession httpSession)
	{
		am=M.getMapper(ActionMapper.class);
		if(request.getMethod()=="POST")
		{
			if(action.getId()==null)//添加
			{
				action.setCreatetime(this.nowTime().toString());
			    am=M.getMapper(ActionMapper.class);
			    //设置动作默认状态
			    action.setStatus(S.getInteger("enactionstatus"));
			    am.insert(action);
			    this.analysisAction("action_edit", request);
			    request.setAttribute("WEB_TITLE", "用户行为添加成功");
				return "home/index";
			}
			else//修改
			{
				//am.del();
				am.updateByPrimaryKeySelective(action);
				request.setAttribute("WEB_TITLE", "修改用户行为成功！");
				//SsmUsers user=(SsmUsers)httpSession.getAttribute("SsmUser");
				this.analysisAction("action_edit", request);
				return "home/index";
			}
			
		}
		else//编辑界面
		{
			Integer id=Integer.parseInt(request.getParameter("actionid"));
			if(0==id)//添加信息
			{
				request.setAttribute("WEB_TITLE", "添加用户行为");
				this.analysisAction("action_edit", request);
				return "Action/edit";
			}
			else//修改信息
			{
				action=am.selectByPrimaryKey(id);
				request.setAttribute("action",action);
				request.setAttribute("WEB_TITLE", "修改用户行为");
				this.analysisAction("action_edit", request);
				return "Action/edit";
			}
		}
	}
	
	@RequestMapping(value="changeStatus")
	public String changeStatus(HttpServletRequest request)
	{
		changeStatus(ActionMapper.class,request);
		return "redirect:index";
	}

}
